package com.qingcloud.adminbackend.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingcloud.adminbackend.anno.CloudApi;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.entity.app.AppDataSource;
import com.qingcloud.adminbackend.entity.app.AppShare;
import com.qingcloud.adminbackend.entity.app.WebPubApp;
import com.qingcloud.adminbackend.entity.sysorg.SysBusinessTypeDTO;
import com.qingcloud.adminbackend.entity.user.SysUser;
import com.qingcloud.adminbackend.entity.usergroup.SysUsergroup;
import com.qingcloud.adminbackend.entity.usergroup.SysUsergroupUser;
import com.qingcloud.adminbackend.pojo.app.*;
import com.qingcloud.adminbackend.service.IAppService;
import com.qingcloud.adminbackend.service.ISysOrgService;
import com.qingcloud.base.annotation.LoginUser;
import com.qingcloud.base.remote.pojo.LoginUserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipFile;

/**
 * 设计器应用管理
 */
@RestController
@RequestMapping("/app")
@Api(value = "应用管理", tags = "应用管理")
public class AppController {

    @Resource
    private IAppService appService;

    @Resource
    private ISysOrgService sysOrgService;

    @CloudApi
    @ApiOperation(value = "导入应用", notes = "导入应用")
    @PostMapping(value = "/import", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public AjaxResult importApp(MultipartFile file) throws IOException {
        String tmpDir = System.getProperty("java.io.tmpdir");
        String destFile = tmpDir + File.separator + IdUtil.getSnowflakeNextIdStr() + ".zip";
        file.transferTo(new File(destFile));
        appService.importApp(new ZipFile(destFile));
        return AjaxResult.ok();
    }

    @CloudApi
    @ApiOperation(value = "分页查询应用")
    @PostMapping("/page")
    public AjaxResult<Page<WebPubApp>> pagePubApp(@RequestBody AppPageParam param) {
        return AjaxResult.ok(appService.pageApp(param));
    }

    @CloudApi
    @ApiOperation(value = "查询用户下的应用")
    @GetMapping("/list")
    public AjaxResult<List<AppDTO>> appList(@LoginUser LoginUserDTO loginUserDTO) {
        List<SysBusinessTypeDTO> list = sysOrgService.getManageBusinessApp(loginUserDTO.getUserId());
        return AjaxResult.ok(list.stream().map(it -> {
            AppDTO appDTO = new AppDTO();
            appDTO.setId(Integer.parseInt(it.getBizCode()));
            appDTO.setName(it.getName());
            return appDTO;
        }).toList());
    }

    @CloudApi
    @ApiOperation(value = "查询应用下的分组信息")
    @GetMapping("/groups/{appId}")
    public AjaxResult<List<SysUsergroup>> userGroupList(@PathVariable("appId") Integer appId) {
        return AjaxResult.ok(appService.userGroupList(appId));
    }

    @CloudApi
    @ApiOperation(value = "未加入组用户查询")
    @PostMapping("/not/user")
    public AjaxResult<List<SysUser>> notInGroupUser(@RequestBody OrgUserParam param, @LoginUser LoginUserDTO loginUserDTO) {
        return AjaxResult.ok(appService.notInGroupUser(param.getOrgId(), param.getGroupId(), loginUserDTO.getUserId()));
    }

    @CloudApi
    @ApiOperation(value = "组里面用户搜索")
    @PostMapping("/user/search")
    public AjaxResult<IPage<AppUserDTO>> searchAppUser(@RequestBody AppUserSearchParam param, @LoginUser LoginUserDTO loginUserDTO) {
        return AjaxResult.ok(appService.searchAppUser(param, loginUserDTO.getUserId()));
    }

    @CloudApi
    @ApiOperation(value = "查询授权角色下拉选项")
    @GetMapping("/roles/{appId}")
    public AjaxResult getAuthRoles(@PathVariable("appId") Integer appId, @LoginUser LoginUserDTO loginUserDTO) {
        return AjaxResult.ok(appService.findAuthRoles(appId, loginUserDTO.getUserId()));
    }

    @CloudApi
    @ApiOperation(value = "批量保存应用用户")
    @PostMapping("/user/save/{groupId}")
    public AjaxResult<Integer> saveAppUser(@PathVariable("groupId") String groupId,
                                           @RequestBody List<SysUsergroupUser> userList, @LoginUser LoginUserDTO loginUserDTO) {
        for (SysUsergroupUser sysUsergroupUser : userList) {
            sysUsergroupUser.setCreateBy(loginUserDTO.getUserId());
        }
        return AjaxResult.ok(appService.saveAppUser(groupId, userList, loginUserDTO.getUserId()));
    }

    @CloudApi
    @ApiOperation(value = "用户应用授权")
    @PostMapping("/auth")
    public AjaxResult<Integer> userAppAuth(@RequestBody SysUsergroupUser usergroupUser, @LoginUser LoginUserDTO loginUserDTO) {
        usergroupUser.setCreateBy(loginUserDTO.getUserId());
        usergroupUser.setModifyBy(loginUserDTO.getUserId());
        return AjaxResult.ok(appService.userAppAuth(usergroupUser));
    }

    @CloudApi
    @ApiOperation(value = "邀请成员分享密钥")
    @PostMapping("/share")
    public AjaxResult<String> getShareKey(@RequestBody AppShare appShare) {
        return AjaxResult.ok(appService.saveAppShare(appShare));
    }

    @CloudApi
    @ApiOperation(value = "添加或修改数据源", notes = "添加或修改数据源")
    @PostMapping("/datasource/save")
    public AjaxResult<AppDataSource> saveDataSource(@RequestBody AppDataSource appDataSource) {
        return AjaxResult.ok(appService.saveDataSource(appDataSource));
    }

    @CloudApi
    @ApiOperation(value = "查询应用数据源详情", notes = "查询应用数据源详情")
    @GetMapping("/datasource/{id}")
    public AjaxResult<AppDataSource> findDataSource(@PathVariable("id") Integer id) {
        return AjaxResult.ok(appService.findDataSource(id));
    }

}
